package com.sunwayworld.basemodule.business.outsourcers.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.outsourcers.dao.LimsOutsourcersProductMainDao;
import com.sunwayworld.basemodule.business.outsourcers.bean.LimsOutsourcersProductMainBean;
import com.sunwayworld.basemodule.business.outsourcers.service.LimsOutsourcersProductMainService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;

@Repository
@GikamBean
public class LimsOutsourcersProductMainServiceImpl implements LimsOutsourcersProductMainService {

    @Autowired
    private LimsOutsourcersProductMainDao limsOutsourcersProductMainDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsOutsourcersProductMainDao getDao() {
        return limsOutsourcersProductMainDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsOutsourcersProductMainBean limsOutsourcersProductMain = jsonWrapper.parseUnique(LimsOutsourcersProductMainBean.class);
        limsOutsourcersProductMain.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsOutsourcersProductMain);
        return limsOutsourcersProductMain.getId();
    }

    @Override
    public JSONObject insertMulti(RestJsonWrapperBean wrapperBean) {
        JSONObject req = new JSONObject();
        List<LimsOutsourcersProductMainBean> beanList = wrapperBean.parse(LimsOutsourcersProductMainBean.class);
        beanList.forEach(b -> {
            b.setId(ApplicationContextHelper.getNextIdentity());
        });
        getDao().insert(beanList);
        return req;
    }
}
